$(function () {
    obj.init();

});

var obj = new function () {
    'use strict';
    return {
        dataGrid:null,
        type: null,
        init:function () {
            this.schoolId = xhTool.getQuery('schoolId');
            this.dataGrid = new xhDataGrid($('#list'), 'school_visit/getSchoolVisit',{schoolId:this.schoolId});
            this.dataGrid.parseToolbar().init().activeButton([], ['bt_list_del']);
            if(this.schoolId == null){ //学校id不存在
                this.dataGrid.command('showColumn','sSchoolName');
                $('#bt_list_add').hide();
            }else {
                $('#select_school').next('.combo').hide();
            }
            this.funObj().bindDataGridEvent();
            if($('#bt_list_checkMenu').length >= 1){
                this.funObj().initMenu();
            }
            this.listener();
        },
        listener:function () {
            var $this = this;

            //新增事件按钮
            $('#bt_list_add').on('click',function () {
                $this.funObj().openAddForm();
            });
            //删除
            $('#bt_list_del').on('click',function () {
                $this.funObj().askDelQuestion();
            });
            //搜索
            $('#bt_list_search').on('click' , function () {
                $this.funObj().search();
            });
            $('#select_school').combobox({
                editable:true,
                filter: function(q, row){
                    var opts = $(this).combobox('options');
                    return row[opts.textField].indexOf(q) >= 0;
                }
            });

        },
        funObj:function () {
            var $this = this;
            return {
                bindDataGridEvent:function () {
                    $this.dataGrid.onDblClickRow = function (rowIndex, rowData) {
                        window.top.xhTop.showModal({
                            title: '编辑参观记录',
                            content: '<iframe  src="'+baseUrl+'school_visit/actForm?iId='+rowData['iId']+'&schoolId='+$this.schoolId+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            width:975,
                            backClose:false
                        });
                    };
                    $this.dataGrid.loadFilter = function (data) {

                        for (var i = 0; i < data.rows.length; i++){
                            var classStyle = '';
                            switch (data.rows[i].iState) {
                                case '审批通过': classStyle = 'grid-cell-text-bg-green';break;
                                case '待审批': classStyle = 'grid-cell-text-bg-gray';break;
                                case '审批不通过': classStyle = 'grid-cell-text-bg-red';break;
                                default : classStyle = '';
                            }
                            data.rows[i].iState = '<div class="'+classStyle+'">'+data.rows[i].iState+'</div>';
                        }
                        return data;
                    }
                    $this.dataGrid.onLoadSuccess = function () {
                        $this.dataGrid.clear();
                    }
                },
                openAddForm:function () {
                    window.top.xhTop.showModal({
                        title: '新增参观记录',
                        content: '<iframe  src="'+baseUrl+'school_visit/actForm?schoolId='+$this.schoolId+'"  style="width: 100%;;border-style: none;padding: 0px;height:500px"  scrolling="yes"   ></iframe>',
                        button:'提交',
                        width:975,
                        backClose:false
                    });
                },
                askDelQuestion:function () {
                    var id = $this.dataGrid.getSelectValue('iId');
                    xhExtra.askChangeStatus('school_visit/delSchoolVisit',{iId:id},null,null,function (res) {
                        window.top.xhTop.showSuccess(res.msg);
                        $this.dataGrid.reload();
                    });
                },
                search:function() {
                    var status = xhExtra.getValue($('#select_status'));
                    var timeType = xhExtra.getValue($('#list_search_timeType'));
                    var startTime = xhExtra.getValue($('#list_search_startTime'));
                    var endTime = xhExtra.getValue($('#list_search_endTime'));
                    $this.dataGrid.reload({schoolId:this.schoolId,status:status,timeType:timeType,startTime:startTime,endTime:endTime});
                },
                initMenu:function () {
                    var menu = $('#bt_list_checkMenu').menubutton({
                        iconCls: 'fa fa-bars fa-lg',
                        menu: '#checkMenu'
                    });
                    $(menu.menubutton('options').menu).menu({
                        onClick: function (item) {
                            //item 的相关属性参见API中的menu、
                            var selections = $this.dataGrid.command('getSelections');
                            if(selections.length > 1 || selections.length <= 0 ){
                                window.top.xhTop.showError('错误','请选择一个活动进行操作');
                                return false;
                            }
                            if(item.text=='审核通过'){
                                $this.funObj().checkAct(true);
                            }else if(item.text=='审核不通过'){
                                $this.funObj().checkAct(false);
                            }
                        }
                    })
                },
                initCity:function (rec) {
                    var pid = rec.value;
                    $('#list_search_city').combobox({
                        url:baseUrl+'school_examine/getDeps/'+pid,
                        valueField: 'id',
                        textField: 'text'
                    });
                },
                checkAct:function (flag) {
                    var  iArtId = $this.dataGrid.getSelectValues('iArtId');
                    if(flag){
                        window.top.xhTop.showModal({
                            title: '审核通过',
                            content: '<iframe  src="'+baseUrl+'article/checkForm/'+flag+'?iArtId='+iArtId+'"  style="width: 100%;;border-style: none;padding: 0px;height:180px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            backClose:false
                        });
                    }else {
                        window.top.xhTop.showModal({
                            title: '审核不通过',
                            content: '<iframe  src="'+baseUrl+'article/checkForm/'+flag+'?iArtId='+iArtId+'"  style="width: 100%;;border-style: none;padding: 0px;height:180px"  scrolling="yes"   ></iframe>',
                            button:'提交',
                            backClose:false
                        });
                    }
                }
            }
        }
    }
};